package com.weiguan.xmpp.util;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class StringUtils {

	public static final String EMPTY = "";
	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

	public static String firstUpcase(String text) {
		if (text != null && text.length() > 0) {
			if (text.length() == 1) {
				return text.toUpperCase();
			}
			return text.substring(0, 1).toUpperCase() + text.substring(1);
		}
		return text;
	}

	public static String getter(String field) {
		return "get" + firstUpcase(field);
	}

	public static String setter(String field) {
		return "set" + firstUpcase(field);
	}

	public static String indent(int idx) {
		StringBuffer result = new StringBuffer(idx * 4 + 1);
		for (int i = 0; i < idx; i++) {
			result.append("    ");
		}
		return result.toString();
	}

	/**
	 * Parse1
	 */

	public static boolean parseBoolean(String s) {
		try {
			return Boolean.parseBoolean(s);
		} catch (Exception e) {
		}
		return false;
	}

	public static int parseInt(String s) {
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return 0;
	}

	public static long parseLong(String s) {
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
		}
		return 0L;
	}

	public static float parseFloat(String s) {
		try {
			return Float.parseFloat(s);
		} catch (Exception e) {
		}
		return 0f;
	}

	public static double parseDouble(String s) {
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {
		}
		return 0d;
	}

	/**
	 * Parse2
	 */

	public static boolean parseBoolean(String s, boolean def) {
		try {
			return Boolean.parseBoolean(s);
		} catch (Exception e) {
		}
		return def;
	}

	public static int parseInt(String s, int def) {
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return def;
	}

	public static long parseLong(String s, long def) {
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
		}
		return def;
	}

	public static float parseFloat(String s, float def) {
		try {
			return Float.parseFloat(s);
		} catch (Exception e) {
		}
		return def;
	}

	public static double parseDouble(String s, double def) {
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {
		}
		return def;
	}

	public static String parseString(Object value, String defaultValue) {
		if (value == null) {
			return defaultValue;
		}
		return value.toString();
	}

	public static int[] parseVersion(String version) {
		int[] ret = { 0, 0, 0 };
		if (version != null) {
			String[] nums = version.replace('.', '-').split("-");
			for (int i = 0; i < nums.length && i < ret.length; i++) {
				ret[i] = parseInt(nums[i], 0);
			}
		}
		return ret;
	}

	public static String toString(Object o) {
		return JSONObject.toJSONString(o);
	}

	/**
	 * 字符串转list
	 * 
	 * @param str
	 * @param sym
	 * @return
	 */
	public static List<Integer> toArray(String str, String sym) {
		List<Integer> list = new ArrayList<Integer>();
		if (StringUtils.isBlank(str)) {
			return list;
		}
		String[] s = str.split(sym);
		for (String v : s) {
			list.add(Integer.parseInt(v));
		}
		return list;
	}

	/**
	 * list转字符串
	 * 
	 * @param list
	 * @param sym
	 * @return
	 */
	public static String arrayToString(List<String> list, String sym) {
		if (list.isEmpty()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (String v : list) {
			sb.append(v).append(sym);
		}
		return sb.substring(0, sb.length() - 1);
	}

	/**
	 * 获取字符串的长度，中文占一个字符,英文数字占半个字符
	 *
	 * @param value
	 *            指定的字符串
	 * @return 字符串的长度
	 */
	public static int length(String value) {
		double valueLength = 0;
		String chinese = "[\u4e00-\u9fa5]";
		// 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1
		for (int i = 0; i < value.length(); i++) {
			// 获取一个字符
			String temp = value.substring(i, i + 1);
			// 判断是否为中文字符
			if (temp.matches(chinese)) {
				// 中文字符长度为2
				valueLength += 2;
			} else {
				// 其他字符长度为1
				valueLength += 1;
			}
		}
		// 进位取整
		return (int) Math.ceil(valueLength);
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isNotEmpty(String str) {
		return !StringUtils.isEmpty(str);
	}

	/**
	 * 判断是否为空或者空串
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotBlank(String str) {
		return !StringUtils.isBlank(str);
	}

	/**
	 * 去除空串
	 * 
	 * @param str
	 * @return
	 */
	public static String trim(String str) {
		return str == null ? null : str.trim();
	}

	public static String trimToNull(String str) {
		String ts = trim(str);
		return isEmpty(ts) ? null : ts;
	}

	public static String trimToEmpty(String str) {
		return str == null ? EMPTY : str.trim();
	}
}
